package com.crb.crm.manage.web.biz.system;

import com.crb.crm.manage.entity.biz.SystemConfigSearchDTO;
import com.crb.crm.manage.entity.biz.SystemConfigVO;
import com.crb.crm.manage.pojo.User;
import com.crb.crm.manage.pojo.biz.SystemConfig;
import com.crb.crm.manage.service.biz.SystemConfigService;
import com.crb.crm.manage.utils.PageDataResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/systemConfig")
public class SystemConfigController {
	
	private static final Logger logger = LoggerFactory
			.getLogger(SystemConfigController.class);
	@Autowired
	private SystemConfigService systemConfigService;
	@RequestMapping("/systemConfigList")
	public String toSystemConfigList() {
		return "biz/system/systemConfigList";
	}
	
	/**
	 * 分页查询字典配置列表
	 * @return ok/fail
	 */
	@RequestMapping(value = "/getSystemConfigs", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions(value="getSystemConfigs")
	public PageDataResult getSystemConfigs(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit, SystemConfigSearchDTO systemConfigSearch) {
		logger.debug("分页查询字典配置列表！搜索条件：systemConfigSearch：" + systemConfigSearch + ",page:" + page
				+ ",每页记录数量limit:" + limit);
//		ErrorController
		PageDataResult pdr = new PageDataResult();
		try {
			if (null == page) {
				page = 1;
			}
			if (null == limit) {
				limit = 10;
			}
			// 获取字典配置
			pdr = systemConfigService.getSystemConfigs(systemConfigSearch, page, limit);
			logger.debug("字典配置列表查询=pdr:" + pdr);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("字典配置列表查询异常！", e);
		}
		return pdr;
	}

	
	/**
	 * 设置字典配置[新增或更新]
	 * @return ok/fail
	 */
	@RequestMapping(value = "/setSystemConfig", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions({"systemConfigAdd","systemConfigEdit"})
	public String setSystemConfig(SystemConfig systemConfig) {
		logger.debug("设置字典配置[新增或更新]！systemConfig:" + systemConfig);
		try {
			if (null == systemConfig) {
				logger.debug("设置字典配置[新增或更新]，结果=请您填写字典配置信息");
				return "请您填写字典配置信息";
			}
			User existUser = (User) SecurityUtils.getSubject().getPrincipal();
			if (null == existUser) {
				logger.debug("设置字典配置[新增或更新]，结果=您未登录或登录超时，请您登录后再试");
				return "您未登录或登录超时，请您登录后再试";
			}
			//user.setInsertUid(existUser.getId());
			// 设置字典配置[新增或更新]
			logger.info("设置字典配置[新增或更新]成功！systemConfig=" + systemConfig + "，操作的用户ID=" + existUser.getId());
			return systemConfigService.setSystemConfig(systemConfig);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("设置字典配置[新增或更新]异常！", e);
			return "操作异常，请您稍后再试";
		}
	}

	/**
	 * 删除字典配置
	 * @return ok/fail
	 */
	@RequestMapping(value = "/delSystemConfig", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("systemConfigDelete")
	public String delSystemConfig(@RequestParam("id") Integer id) {
		logger.debug("删除字典配置！id:" + id);
		String msg = "";
		try {
			if (null == id) {
				logger.debug("删除字典配置，结果=请求参数有误，请您稍后再试");
				return "请求参数有误，请您稍后再试";
			}
			User existUser = (User) SecurityUtils.getSubject().getPrincipal();
			if (null == existUser) {
				logger.debug("删除字典配置，结果=您未登录或登录超时，请您登录后再试");
				return "您未登录或登录超时，请您登录后再试";
			}
			// 删除字典配置
			msg = systemConfigService.setDelSystemConfig(id, 1);
			logger.info("删除字典配置:" + msg + "。userId=" + id + "，操作用户id:"+ existUser.getId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除字典配置异常！", e);
			msg = "操作异常，请您稍后再试";
		}
		return msg;
	}

	/**
	 *
	 * @描述：恢复字典配置
	 * @创建人：wyait
	 * @创建时间：2018年4月27日 上午9:49:14
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/recoverSystemConfig", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("systemConfigDelete")
	public String recoverSystemConfig(@RequestParam("id") Integer id) {
		logger.debug("恢复字典配置！id:" + id);
		String msg = "";
		try {
			User existUser = (User) SecurityUtils.getSubject().getPrincipal();
			if (null == existUser) {
				return "您未登录或登录超时，请您登录后再试";
			}
			if (null == id ) {
				return "请求参数有误，请您稍后再试";
			}
			// 删除字典配置
			msg = systemConfigService.setDelSystemConfig(id, 0);
			logger.info("恢复字典配置【" + this.getClass().getName() + ".recoverSystemConfig】"
					+ msg + "。字典配置Id=" + id + "，操作的用户ID=" + existUser.getId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("恢复字典配置【" + this.getClass().getName()
					+ ".recoverUser】字典配置异常！", e);
			msg = "操作异常，请您稍后再试";
		}
		return msg;
	}

	/**
	 * 查询字典配置数据
	 * @return map
	 */
	@RequestMapping(value = "/getSystemConfig", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getSystemConfig(@RequestParam("id") Integer id) {
		logger.debug("查询字典配置数据！id:" + id);
		Map<String, Object> map = new HashMap<>();
		try {
			if (null == id) {
				logger.debug("查询字典配置数据==请求参数有误，请您稍后再试");
				map.put("msg", "请求参数有误，请您稍后再试");
				return map;
			}
			// 查询字典配置
			SystemConfigVO systemConfigvo = systemConfigService.getSystemConfig(id);
			logger.debug("查询字典配置数据！systemConfigvo=" + systemConfigvo);
			if (null != systemConfigvo) {
				map.put("systemConfig", systemConfigvo);
				map.put("msg", "ok");
			} else {
				map.put("msg", "查询字典配置信息有误，请您稍后再试");
			}
			logger.debug("查询字典配置数据成功！map=" + map);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "查询字典配置错误，请您稍后再试");
			logger.error("查询字典配置数据异常！", e);
		}
		return map;
	}
}
